/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.network;

import com.jme3.network.Message;
import com.jme3.network.message.GZIPCompressedMessage;
import etherblocks.engine.*;
import etherblocks.engine.network.handlers.*;
import etherblocks.engine.network.messages.*;
import etherblocks.engine.states.*;
import etherblocks.game.World;
import etherblocks.game.entitysystem.EntityMap;
import com.cubes.*;

/**
 *
 * @author Carl
 */
public class GameClient{

    public GameClient(){
        
    }
    protected NetworkClient networkClient;
    protected MainApplication mainApplication;
    private BlockTerrainMessageHandler blockTerrainHandler;
    private EntityMap entityMap = new EntityMap();
    private int playerEntityID;

    public void onNetworkMessageReceived(Message receivedMessage){
        WorldAppState worldAppState = mainApplication.getStateManager().getState(WorldAppState.class);
        receivedMessage = decompressMessage(receivedMessage);
        if(receivedMessage instanceof Message_BlockSetEvent){
            Message_BlockSetEvent message = (Message_BlockSetEvent) receivedMessage;
            worldAppState.getWorld().getBlockTerrain().setBlock(message.getX(), message.getY(), message.getZ(), BlockManager.getClass(message.getBlockType()));
        }
        else if(receivedMessage instanceof Message_BlockRemoveEvent){
            Message_BlockRemoveEvent message = (Message_BlockRemoveEvent) receivedMessage;
            worldAppState.getWorld().getBlockTerrain().removeBlock(message.getX(), message.getY(), message.getZ());
        }
        else if((receivedMessage instanceof Message_EntityComponentAdded) && (!networkClient.isServer())){
            Message_EntityComponentAdded message = (Message_EntityComponentAdded) receivedMessage;
            entityMap.setComponent(message.getID(), message.getComponent());
        }
        else if((receivedMessage instanceof Message_EntityComponentRemoved) && (!networkClient.isServer())){
            Message_EntityComponentRemoved message = (Message_EntityComponentRemoved) receivedMessage;
            entityMap.clearComponent(message.getID(), message.getComponent().getClass());
        }
        else if((receivedMessage instanceof Message_EntityAdded) && (!networkClient.isServer())){
            Message_EntityAdded message = (Message_EntityAdded) receivedMessage;
            entityMap.createNewEntity(message.getID());
        }
        else if((receivedMessage instanceof Message_EntityRemoved) && (!networkClient.isServer())){
            Message_EntityRemoved message = (Message_EntityRemoved) receivedMessage;
            entityMap.removeEntity(message.getID());
        }
        else if(receivedMessage instanceof Message_BlockChunk){
            final Message_BlockChunk message = (Message_BlockChunk) receivedMessage;
            blockTerrainHandler.onMessageReceived(message);
        }
        else if(receivedMessage instanceof Message_ConnectionSuccessful){
            Message_ConnectionSuccessful message = (Message_ConnectionSuccessful) receivedMessage;
            playerEntityID = message.getPlayerEntityID();
            World world = new World();
            BlockTerrainControl blockTerrain = new BlockTerrainControl(CubesConfiguration.getSettings(mainApplication), new Vector3Int(7, 1, 7));
            world.setBlockTerrain(blockTerrain);
            world.setEntityMap(entityMap);
            blockTerrainHandler = new BlockTerrainMessageHandler(blockTerrain);
            mainApplication.getStateManager().attach(new WorldAppState(world));
            mainApplication.getStateManager().attach(new TexturePackAppState());
            mainApplication.getStateManager().attach(new EntitySystemAppState());
        }
        else if(receivedMessage instanceof Message_ConnectionFinished){
            Message_ConnectionFinished message = (Message_ConnectionFinished) receivedMessage;
            mainApplication.getStateManager().attach(new PlayerAppState());
        }
    }
    
    private static Message decompressMessage(Message message){
        if(message instanceof GZIPCompressedMessage){
            GZIPCompressedMessage compressedMessage = (GZIPCompressedMessage) message;
            return compressedMessage .getMessage();
        }
        return message;
    }

    public NetworkClient getNetworkClient(){
        return networkClient;
    }

    public void setNetworkClient(NetworkClient networkClient){
        this.networkClient = networkClient;
    }

    public MainApplication getMainApplication(){
        return mainApplication;
    }

    public void setMainApplication(MainApplication mainApplication){
        this.mainApplication = mainApplication;
    }

    public EntityMap getEntityMap(){
        return entityMap;
    }

    public int getPlayerEntityID(){
        return playerEntityID;
    }
}
